﻿using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Configuration;

using SBPweb.RIA.Shared.Contracts.Services;

namespace SBPweb.RIA.Server.Common.Communication
{

    public class BasicServiceHostFactory<TService, TContract> : ServiceHostFactoryBase
        where TService : IService
        where TContract : IService
    {

        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            Uri baseAddress = baseAddresses[0];
            string dnsSafeHost = ConfigurationManager.AppSettings["dnsSafeHost"];
            if (!string.IsNullOrEmpty(dnsSafeHost))
            {
                foreach (Uri uri in baseAddresses)
                {
                    if (dnsSafeHost == uri.DnsSafeHost)
                    {
                        baseAddress = uri;
                        break;
                    }
                }
            }
            return CreateCustomHostInstance(baseAddresses);
        }

        protected virtual CustomHost CreateCustomHostInstance(Uri[] baseAddresses)
        {
            return new CustomHost(typeof(TService), typeof(TContract), baseAddresses);
        }

    }
    
    public class BasicServiceHostFactory<TService> : BasicServiceHostFactory<TService, TService>
        where TService : IService
    {
    }

}